package com.grandtech.insurance.common.db.jdbc.pg;

import com.grandtech.insurance.common.db.jdbc.sql.TransferUtil;
import com.grandtech.insurance.common.utils.trans.Transformation;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.Geometry;
import org.json.JSONObject;

import java.util.*;

/**
 * Created by zy on 2018/5/1.
 */

public class Feature2SqlUtil {

    /**
     * @param tableStructure
     * @return
     */
    public static String spatialMap2Sql(Map<String, Object> tableStructure) {
        if (tableStructure == null || tableStructure.size() == 0) return null;
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> columns = (List<Map>) tableStructure.get("columns");
        StringBuilder sqlBuilder = new StringBuilder("select ");
        String name;
        //int pk;
        for (int i = 0; i < columns.size(); i++) {
            name = (String) columns.get(i).get("name");
            if (name.toLowerCase().contains("shape") || name.toLowerCase().contains("geom")) {
                sqlBuilder.append("ST_AsGeoJson(shape) as shape").append(",");
            } else {
                sqlBuilder.append(name).append(",");
            }
        }
        if (sqlBuilder != null && sqlBuilder.toString().endsWith(",")) {
            sqlBuilder.delete(sqlBuilder.toString().lastIndexOf(","), sqlBuilder.toString().length());
        }
        sqlBuilder.append(" from ").append(tableName);
        return sqlBuilder.toString();
    }

    public static String spatialMap2Sql(Map<String, Object> tableStructure, boolean p2p) {
        if (tableStructure == null || tableStructure.size() == 0) return null;
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> columns = (List<Map>) tableStructure.get("columns");
        StringBuilder sqlBuilder = new StringBuilder("select ");
        String name;
        //int pk;
        for (int i = 0; i < columns.size(); i++) {
            name = (String) columns.get(i).get("name");
            if (name.toLowerCase().contains("shape") || name.toLowerCase().contains("geom")) {
                if (p2p) {
                    // sqlBuilder.append("ST_AsGeoJson(ST_Centroid(shape)) as shape").append(",");st_astext(
                    sqlBuilder.append("st_astext(ST_Centroid(shape)) as wkt").append(",");
                } else {
                    sqlBuilder.append("ST_AsGeoJson(shape) as shape").append(",");
                }
            } else {
                sqlBuilder.append(name).append(",");
            }
        }
        if (sqlBuilder != null && sqlBuilder.toString().endsWith(",")) {
            sqlBuilder.delete(sqlBuilder.toString().lastIndexOf(","), sqlBuilder.toString().length());
        }
        sqlBuilder.append(" from ").append(tableName);
        return sqlBuilder.toString();
    }

    public static String spatialMap2SqlByMap(Map<String, Object> tableStructure, Map<String, Object> mapCondition) {
        StringBuilder sql = new StringBuilder();
        String allSql = spatialMap2Sql(tableStructure);
        String condition = TransferUtil.map2SimpleWhere(mapCondition);
        sql.append(allSql).append(" where ").append(condition);
        return sql.toString();
    }

    public static String spatialMap2SqlByField(Map<String, Object> tableStructure, List<Feature> features, String... fields) {
        StringBuilder stringBuilder = new StringBuilder();
        String allSql = spatialMap2Sql(tableStructure);
        String condition;
        if (fields != null && fields.length > 0) {
            condition = features2WhereConditions(features, fields);
        } else {
            condition = features2WhereConditions(tableStructure, features);
        }
        stringBuilder.append(allSql).append(" where ").append(condition);
        return stringBuilder.toString();
    }

    public static String spatialMap2SqlByWhere(Map<String, Object> tableStructure, String condition) {
        StringBuilder stringBuilder = new StringBuilder();
        String allSql = spatialMap2Sql(tableStructure);
        stringBuilder.append(allSql).append(" where ").append(condition);
        return stringBuilder.toString();
    }

    public static String spatialMap2SqlByWhere(Map<String, Object> tableStructure, String condition, boolean p2p) {
        StringBuilder stringBuilder = new StringBuilder();
        String allSql = spatialMap2Sql(tableStructure, p2p);
        stringBuilder.append(allSql).append(" where ").append(condition);
        return stringBuilder.toString();
    }

    public static String spatialMap2SqlByFeature(Map<String, Object> tableStructure, Feature feature) {
        StringBuilder stringBuilder = new StringBuilder();
        String allSql = spatialMap2Sql(tableStructure);
        String condition = featureSimpleWhere(tableStructure, feature);
        stringBuilder.append(allSql).append(" where ").append(condition);
        return stringBuilder.toString();
    }

    private static String featureSimpleWhere(Map<String, Object> tableStructure, Feature feature) {
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        String pkName = (String) tableInfo.get("pkName");
        Object val = FeatureUtil.getFeatureAttributeAsObject(feature, pkName);
        if (val == null) return null;
        StringBuilder condition = new StringBuilder();
        if (val instanceof String) {
            condition.append(pkName).append(" = ").append("'").append(val).append("',");
        } else {
            condition.append(pkName).append(" = ").append(val).append(",");
        }
        if (condition != null && condition.toString().endsWith(",")) {
            condition.delete(condition.toString().lastIndexOf(","), condition.toString().length());
        }
        return condition.toString();
    }

    /**
     * 主键获取
     *
     * @param tableStructure
     * @param objectid
     * @return
     */
    public static String spatialMap2Sql(Map<String, Object> tableStructure, Object objectid) {
        if (tableStructure == null || tableStructure.size() == 0) return null;
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> columns = (List<Map>) tableStructure.get("columns");
        StringBuilder sqlBuilder = new StringBuilder("select ");
        String name;
        for (int i = 0; i < columns.size(); i++) {
            name = (String) columns.get(i).get("name");
             if(name.toLowerCase().contains("geom")){sqlBuilder.append("ST_AsGeoJson(geom) as geom").append(",");}
            else {
                sqlBuilder.append(name).append(",");
            }
        }
        if (sqlBuilder != null && sqlBuilder.toString().endsWith(",")) {
            sqlBuilder.delete(sqlBuilder.toString().lastIndexOf(","), sqlBuilder.toString().length());
        }
        sqlBuilder.append(" from ").append(tableName).append(" where ").append(tableInfo.get("pkName")).append(" = ");
        if (objectid != null && objectid instanceof String) {
            sqlBuilder.append("'").append(objectid).append("'");
        } else {
            sqlBuilder.append(objectid);
        }
        return sqlBuilder.toString();
    }
    //==================================分割线===================================

    /**
     * @param tableStructure
     * @param feature
     * @return
     */
    public static String spatialFeature2InsertSql(Map<String, Object> tableStructure, Feature feature) {
        if (tableStructure == null && feature == null) return null;
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> dbColumns = (List<Map>) tableStructure.get("columns");

        StringBuffer sql = new StringBuffer();
        StringBuffer columns = new StringBuffer();
        StringBuffer values = new StringBuffer();
        Geometry geometry = feature.geometry();
        String wkt = Transformation.boxGeometry2Wkt(geometry);
        columns.append("(");
        values.append("(");
        columns.append(tableInfo.get("geoColumnName")).append(",");
        values.append("ST_GeomFromText('").append(wkt).append("',4326)").append(",");
        /**
         * 循环表的列
         */
        Map map;
        String name;
        String type;
        int pk;
        Object val;
        for (int i = 0; i < dbColumns.size(); i++) {
            map = dbColumns.get(i);
            pk = map.get("pk") == null ? 0 : (int) map.get("pk");
            name = (String) map.get("name");
            type = (String) map.get("type");
            if (pk == 1 && name.equals("objectid")) {
                continue;
            } else {
                val = FeatureUtil.getFeatureAttributeAsObject(feature, name);
                if (val == null) continue;
                if (val instanceof String) {
                    values.append("'").append(val).append("',");
                } else {
                    values.append(val).append(",");
                }
                columns.append(name).append(",");
            }
        }
        if (columns != null && columns.toString().endsWith(",")) {
            columns.delete(columns.toString().lastIndexOf(","), columns.toString().length());
        }
        if (values != null && values.toString().endsWith(",")) {
            values.delete(values.toString().lastIndexOf(","), values.toString().length());
        }
        columns.append(")");
        values.append(")");
        String sqlStr = sql.append("insert into ").append(tableName).append(columns).append(" values ").append(values).toString();
        return sqlStr;
    }

    /**
     * 不更新主键
     *
     * @param tableStructure
     * @param feature
     * @return
     */
    public static String spatialFeature2UpdateSql(Map<String, Object> tableStructure, Feature feature) {
        if (tableStructure == null && feature == null) return null;
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> dbFields = (List<Map>) tableStructure.get("columns");

        StringBuilder sql = new StringBuilder();
        StringBuilder columns = new StringBuilder();
        StringBuilder condition = new StringBuilder();
        Geometry geometry = feature.geometry();
        String shape = tableInfo.get("geoColumnName").toString();
        String wkt = Transformation.boxGeometry2Wkt(geometry);
        columns.append(shape).append("=").append("ST_GeomFromText('").append(wkt).append("',4326)").append(",");

        Map map;
        String name;
        String type;
        int pk;
        Object v;
        for (int i = 0; i < dbFields.size(); i++) {
            map = dbFields.get(i);
            pk = map.get("pk") == null ? 0 : (int) map.get("pk");
            name = (String) map.get("name");
            type = (String) map.get("type");
            v = FeatureUtil.getFeatureAttributeAsObject(feature, name);
            if (v == null) continue;
            if (pk == 1 && name.equals("objectid")) {
                if (v instanceof String) {
                    condition.append(name).append("=").append("'").append(v.toString()).append("',");
                } else {
                    condition.append(name).append("=").append(v).append(",");
                }
                continue;
            } else {
                if (v instanceof String) {
                    columns.append(name).append("=").append("'").append(v.toString()).append("',");
                } else {
                    columns.append(name).append("=").append(v).append(",");
                }
            }
        }
        if (columns != null && columns.toString().endsWith(",")) {
            columns.delete(columns.toString().lastIndexOf(","), columns.toString().length());
        }
        if (condition != null && condition.toString().endsWith(",")) {
            condition.delete(condition.toString().lastIndexOf(","), condition.toString().length());
        }
        return sql.append("update ").append(tableName).append(" set ").append(columns).append(" where ").append(condition).toString();
    }

    public static String spatialFeature2UpdateSql(Map<String, Object> tableStructure, Feature feature, Map mapCondition) {
        if (tableStructure == null && feature == null) return null;
        if (mapCondition == null) return spatialFeature2UpdateSql(tableStructure, feature);

        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> dbFields = (List<Map>) tableStructure.get("columns");

        StringBuilder sql = new StringBuilder();
        StringBuilder columns = new StringBuilder();
        Geometry geometry = feature.geometry();
        String shape = tableInfo.get("geoColumnName").toString();
        String wkt = Transformation.boxGeometry2Wkt(geometry);
        columns.append(shape).append("=").append("ST_GeomFromText('").append(wkt).append("',4326)").append(",");

        Map map;
        String name;
        String type;
        int pk;
        Object v;
        for (int i = 0; i < dbFields.size(); i++) {
            map = dbFields.get(i);
            pk = map.get("pk") == null ? 0 : (int) map.get("pk");
            name = (String) map.get("name");
            type = (String) map.get("type");
            v = FeatureUtil.getFeatureAttributeAsObject(feature, name);
            if (v == null) continue;
            if (pk == 1 && name.equals("objectid")) {
                continue;
            } else {
                if (v instanceof String) {
                    columns.append(name).append("=").append("'").append(v.toString()).append("',");
                } else {
                    columns.append(name).append("=").append(v).append(",");
                }
            }
        }
        if (columns != null && columns.toString().endsWith(",")) {
            columns.delete(columns.toString().lastIndexOf(","), columns.toString().length());
        }
        String conditionStr = TransferUtil.map2SimpleWhere(mapCondition);
        return sql.append("update ").append(tableName).append(" set ").append(columns).append(" where ").append(conditionStr).toString();
    }

    public static String spatialFeature2UpdateSql(Map<String, Object> tableStructure, Feature feature, String conditionStr) {
        if (tableStructure == null && feature == null) return null;
        if (conditionStr == null) return spatialFeature2UpdateSql(tableStructure, feature);

        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> dbFields = (List<Map>) tableStructure.get("columns");

        StringBuilder sql = new StringBuilder();
        StringBuilder columns = new StringBuilder();
        StringBuilder condition = new StringBuilder();
        Geometry geometry = feature.geometry();
        String shape = tableInfo.get("geoColumnName").toString();
        String wkt = Transformation.boxGeometry2Wkt(geometry);
        columns.append(shape).append("=").append("ST_GeomFromText('").append(wkt).append("',4326)").append(",");

        Map map;
        String name;
        String type;
        int pk;
        Object v;
        for (int i = 0; i < dbFields.size(); i++) {
            map = dbFields.get(i);
            pk = map.get("pk") == null ? 0 : (int) map.get("pk");
            name = (String) map.get("name");
            type = (String) map.get("type");
            v = FeatureUtil.getFeatureAttributeAsObject(feature, name);
            if (v == null) continue;
            if (pk == 1 && name.equals("objectid")) {
                continue;
            } else {
                if (v instanceof String) {
                    columns.append(name).append("=").append("'").append(v.toString()).append("',");
                } else {
                    columns.append(name).append("=").append(v).append(",");
                }
            }
        }
        if (columns != null && columns.toString().endsWith(",")) {
            columns.delete(columns.toString().lastIndexOf(","), columns.toString().length());
        }
        if (conditionStr != null) {
            return sql.append("update ").append(tableName).append(" set ").append(columns).append(" where ").append(conditionStr).toString();
        }
        return sql.append("update ").append(tableName).append(" set ").append(columns).append(" where ").append(condition).toString();
    }

    //==================================分割线===================================

    /**
     * 主键删
     *
     * @param tableStructure
     * @param objId
     * @return
     */
    public static String spatialDelSql(Map<String, Object> tableStructure, Object objId) {
        StringBuilder sql = new StringBuilder();
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        if (objId instanceof Integer) {
            return sql.append("delete from ").append(tableName).append(" where ").append(tableInfo.get("pkName")).append(" = ").append(objId).toString();
        } else {
            return sql.append("delete from ").append(tableName).append(" where ").append(tableInfo.get("pkName")).append(" = '").append(objId).append("'").toString();
        }
    }

    /**
     * 要素删
     *
     * @param tableStructure
     * @param feature
     * @return
     */
    public static String spatialFeature2DelSql(Map<String, Object> tableStructure, Feature feature) {
        StringBuilder sql = new StringBuilder();
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        if (tableInfo == null || tableInfo.size() == 0) return null;
        String pkName = tableInfo.get("pkName").toString();
        String pkType = tableInfo.get("pkType").toString();
        Object objId = feature.getProperty(pkName);
        if (pkType.equals("integer")) {
            return sql.append("delete from ").append(tableName).append(" where ").append(tableInfo.get("pkName")).append(" = ").append(objId).toString();
        } else {
            return sql.append("delete from ").append(tableName).append(" where ").append(tableInfo.get("pkName")).append(" = '").append(objId).append("'").toString();
        }
    }

    /**
     * 要素删
     *
     * @param tableStructure
     * @return
     */
    public static String spatialFeature2DelSql(Map<String, Object> tableStructure, Map mapCondition) {
        StringBuilder sql = new StringBuilder();
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        String where = TransferUtil.map2SimpleWhere(mapCondition);
        return sql.append("delete from ").append(tableName).append(" where ").append(where).toString();
    }


    public static Feature map2Feature(Map tableStructure, Map featureMap) {
        //String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        String shape = tableInfo.get("geoColumnName").toString();
        JSONObject feature = new JSONObject();
        JSONObject properties = new JSONObject();
        try {
            String geoJson = featureMap.get(shape).toString();
            feature.put("type", "Feature");
            feature.put("properties", properties);
            Object val;
            for (Object key : featureMap.keySet()) {
                if ( key.toString().toLowerCase().contains("geom")) {
                    feature.put("geometry", new JSONObject(geoJson));
                    continue;
                }
                val = featureMap.get(key);
                if (val == null) {
                    properties.put(key.toString(), JSONObject.NULL);
                    continue;
                }
                if (val instanceof String) {
                    properties.put(key.toString(), val.toString());
                }
                if (val instanceof Number) {
                    properties.put(key.toString(), val);
                }
                if (val instanceof Date) {
                    properties.put(key.toString(), val);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
        String json = feature.toString();
        return Feature.fromJson(json);
    }

    public static List<Feature> map2ListFeature(Map tableStructure, List<Map> list) {
        if (list == null) return null;
        List<Feature> features = new ArrayList<>();
        Feature feature;
        for (int i = 0; i < list.size(); i++) {
            feature = map2Feature(tableStructure, list.get(i));
            features.add(feature);
        }
        return features;
    }


    /**
     * 条件获取
     *
     * @param tableStructure
     * @param condition
     * @return
     */
    public static String spatialMap2Sql(Map<String, Object> tableStructure, Map<String, Object> condition) {
        if (tableStructure == null || tableStructure.size() == 0) return null;
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> columns = (List<Map>) tableStructure.get("columns");
        StringBuilder sqlBuilder = new StringBuilder("select ");
        String name;
        for (int i = 0; i < columns.size(); i++) {
            name = (String) columns.get(i).get("name");
            if (name.toLowerCase().contains("shape") || name.toLowerCase().contains("geom")) {
                sqlBuilder.append("ST_AsGeoJson(shape) as shape").append(",");
            } else {
                sqlBuilder.append(name).append(",");
            }
        }
        if (sqlBuilder != null && sqlBuilder.toString().endsWith(",")) {
            sqlBuilder.delete(sqlBuilder.toString().lastIndexOf(","), sqlBuilder.toString().length());
        }
        sqlBuilder.append(" from ").append(tableName).append(" where ").append(" 1=1 ");
        for (String c : condition.keySet()) {
            Object o = condition.get(c);
            if (o != null && o instanceof String) {
                sqlBuilder.append(" and ").append(c).append(" = '").append(o).append("'");
            } else {
                sqlBuilder.append(" and ").append(c).append(" = ").append(o);
            }
        }

        return sqlBuilder.toString();
    }

//=============================================================================

    /**
     * @param tableStructure
     * @param expression
     * @return
     */
    public static String spatialMap2CollectSymbolSql(Map<String, Object> tableStructure, Map<String, String> expression) {
        if (tableStructure == null || tableStructure.size() == 0) return null;
        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> columns = (List<Map>) tableStructure.get("columns");
        StringBuilder sqlBuilder = new StringBuilder("select ");
        String _field = null;
        String _expression = null;
        if (expression != null) {
            for (String key : expression.keySet()) {
                _field = key;
                _expression = expression.get(_field);
                break;
            }
        }
        String name;
        for (int i = 0; i < columns.size(); i++) {
            name = (String) columns.get(i).get("name");
            if (name.toLowerCase().contains("shape") || name.toLowerCase().contains("geom")) {
                sqlBuilder.append("AsBinary(Centroid(shape)) as shape").append(",");
            } else {
                if (name.equals(_field)) {
                    sqlBuilder.append(_expression).append(" as ").append(_field).append(",");
                } else {
                    sqlBuilder.append(name).append(",");
                }
            }

        }
        if (sqlBuilder != null && sqlBuilder.toString().endsWith(",")) {
            sqlBuilder.delete(sqlBuilder.toString().lastIndexOf(","), sqlBuilder.toString().length());
        }
        sqlBuilder.append(" from ").append(tableName);
        return sqlBuilder.toString();
    }

    /**
     * @param tableStructure
     * @param feature
     * @param expression
     * @return
     */
    public static String spatialMap2SingleSymbolSql(Map<String, Object> tableStructure, Feature feature, Map<String, String> expression) {
        if (tableStructure == null || tableStructure.size() == 0) return null;

        String tableName = (String) tableStructure.get("tableName");
        Map tableInfo = (Map) tableStructure.get("tableInfo");
        List<Map> columns = (List<Map>) tableStructure.get("columns");

        StringBuilder sqlBuilder = new StringBuilder("select ");
        String pkName = tableInfo.get("pkName").toString();
        Object pkValue = feature.getProperty(pkName);
        if (pkValue == null) return null;
        StringBuilder sqlWhere = new StringBuilder(" where ").append(pkName).append(" = ");
        if (pkValue instanceof String) {
            sqlWhere.append("'").append(pkValue).append("'");
        } else {
            sqlWhere.append(pkValue);
        }
        String _field = null;
        String _expression = null;
        if (expression != null) {
            for (String key : expression.keySet()) {
                _field = key;
                _expression = expression.get(_field);
                break;
            }
        }
        String name;
        for (int i = 0; i < columns.size(); i++) {
            name = (String) columns.get(i).get("name");
            if (name.toLowerCase().contains("shape") || name.toLowerCase().contains("geom")) {
                sqlBuilder.append("AsBinary(Centroid(shape)) as shape").append(",");
            } else {
                if (name.equals(_field)) {
                    sqlBuilder.append(_expression).append(" as ").append(_field).append(",");
                } else {
                    sqlBuilder.append(name).append(",");
                }
            }
        }
        if (sqlBuilder != null && sqlBuilder.toString().endsWith(",")) {
            sqlBuilder.delete(sqlBuilder.toString().lastIndexOf(","), sqlBuilder.toString().length());
        }
        sqlBuilder.append(" from ").append(tableName).append(sqlWhere);
        return sqlBuilder.toString();
    }

    /**
     * feature属性们 转map
     */
    public static Map featureFields2Map(Feature feature, String... fields) {
        if (feature == null || fields == null) return null;
        String fieldKey;
        Object fieldVal;
        Map<String, Object> condition = new HashMap<>();
        for (int i = 0; i < fields.length; i++) {
            fieldKey = fields[i];
            fieldVal = FeatureUtil.getFeatureAttributeAsObject(feature, fieldKey);
            condition.put(fieldKey, fieldVal);
        }
        return condition;
    }

    /**
     * feature属性们 转where
     */
    public static String featureFields2WhereContext(Feature feature, String... fields) {
        Map mapCondition = featureFields2Map(feature, fields);
        String condition = TransferUtil.map2SimpleWhere(mapCondition);
        return condition;
    }

    /**
     * @param features
     * @param fields
     * @return (a = 1 and b = 1) or (a =2 and b=3)
     */
    public static String features2WhereConditions(List<Feature> features, String... fields) {
        StringBuilder stringBuilder = new StringBuilder();
        if (features == null) return " 1 = 2 ";
        Feature feature;
        for (int i = 0; i < features.size(); i++) {
            feature = features.get(i);
            Map mapCondition = featureFields2Map(feature, fields);
            String itemCondition = TransferUtil.map2SimpleWhere(mapCondition);
            stringBuilder.append("(").append(itemCondition).append(")");
            if (i != features.size() - 1) {
                stringBuilder.append("or");
            }
        }
        return stringBuilder.toString();
    }


    public static String features2WhereConditions(Map<String, Object> tableStructure, List<Feature> features) {
        StringBuilder stringBuilder = new StringBuilder();
        if (features == null) return " 1 = 2 ";
        Feature feature;
        String itemCondition;
        for (int i = 0; i < features.size(); i++) {
            feature = features.get(i);
            itemCondition = featureSimpleWhere(tableStructure, feature);
            stringBuilder.append("(").append(itemCondition).append(")");
            if (i != features.size() - 1) {
                stringBuilder.append("or");
            }
        }
        return stringBuilder.toString();
    }
}
